<?php

namespace app\common\service;

use app\common\enum\transaction\SourceTypeEnum;
use app\common\enum\transaction\TypeEnum;
use app\http\exception\ServerErrorHttpException;
use app\sign\model\UserSign;
use app\http\exception\ApiException;
use Exception;
use randy\tools\units\ExtendQueryHelper;
use think\db\exception\DataNotFoundException;
use think\db\exception\DbException;
use think\db\exception\ModelNotFoundException;

class UserSignService extends BaseService
{
    public function getUserSignList(ExtendQueryHelper $extendQueryHelper, $data)
    {
        $query = UserSign::build()
                         ->where($extendQueryHelper->where)
                         ->order($extendQueryHelper->sort, $extendQueryHelper->order);
        $list = $extendQueryHelper->is_page ? $query->_paginate() : $query->select();
        return $list;
    }

    public function getUserSignItem($id)
    {
        return UserSign::build()
                       ->findOrFail($id);
    }

    public function delUserSignItem($id)
    {
        $info = UserSign::find($id);
        return $info->delete();
    }

    public function setUserSignItem($id, $data)
    {
        $info = UserSign::find($id);
        $info->save($data);
        return $info;
    }


    /**
     * @throws DataNotFoundException
     * @throws ModelNotFoundException
     * @throws DbException
     */
    public function addUserSignItem($data)
    {
        $model = new UserSign();
        $model->startTrans();

        try {
            $model->user_sign_uuid = uuid();
            if (!is_client()) {
                error_abort('当前应用类型无法操作');
            }
            if (
                UserSign::build()
                        ->where('user_id', get_client_id())
                        ->where('sign_date', date('Y-m-d'))
                        ->find()) {
                throw new ServerErrorHttpException('您今天已签到,请勿重复签到');
            }
            $yesterday = date('Y-m-d', strtotime('-1 day'));
            $yesterdaySign = UserSign::build()
                                     ->where('user_id', get_client_id())
                                     ->where('sign_date', $yesterday)
                                     ->find();
            $model->week = date('w') ?: 7;
            if (!$yesterdaySign) {
                $model->duration_days = 1;
            }
            if ($yesterdaySign) {
                if ($model->week == 1) {
                    $model->duration_days = 1;
                } else {
                    $model->duration_days = $yesterdaySign->duration_days + 1;
                }
            }//获取签到积分数
            if ($model->duration_days == 7) {
                $points = get_setting('连续签到7天可获得积分数');
            } else {
                $points = get_setting('每日签到可获得积分数');
            }
            $model->user_id = get_client_id();
            $model->sign_date = date('Y-m-d');
            $model->week = date('w') ?: 7;
            $model->points = $points;
            $model->save();// 积分上账
            UserMoneyService::changeUserPoint($points, get_client_id(), TypeEnum::SR->value, $model->user_sign_uuid, SourceTypeEnum::QD->value, "用户签到送积分", '用户签到');
            $model->commit();
            return $model;
        } catch (Exception $e) {
            $model->rollback();
            throw $e;
        }
    }


}
